home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 26
/
Cream of the Crop 26.iso
/
program
/
ddj0897.zip
/
EMPTY.ASC
< prev
next >
Wrap
Text File
|
1997-06-20
|
2KB
|
79 lines
_The Empty Member C++ Optimization_
by Nathan Myers
Listing One
template <class T>
class allocator { // an empty class
static T* allocate(size_t n)
{ return (T*) ::operator new(n * sizeof T); }
. . .
};
Listing Two
template <class T, class Alloc = allocator<T> >
class list {
Alloc alloc_;
struct Node { . . . };
Node* head_;
public:
explicit list(Alloc const& a = Alloc())
: alloc_(a) { . . . }
. . .
};
Listing Three
struct Bar { };
struct Foo {
struct Bar a[2];
struct Bar b;
};
Foo f;
Listing Four
template <class T, class Alloc = allocator<T> >
class list : private Alloc {
struct Node { . . . };
Node* head_;
public:
explicit list(Alloc const& a = Alloc())
: Alloc(a) { . . . }
. . .
};
Listing Five
template <class T, class Alloc = allocator<T> >
class list {
struct Node { . . . };
struct P : public Alloc {
P(Alloc const& a) : Alloc(a), p(0) { }
Node* p;
};
P head_;
public:
explicit list(Alloc const& a = Alloc())
: head_(a) { . . . }
. . .
};
Listing Six
template <class Base, class Member>
struct BaseOpt : Base {
Member m;
BaseOpt(Base const& b, Member const& mem)
: Base(b), m(mem) { }
};
Listing Seven
template <class T, class Alloc = allocator<T> >
class list {
struct Node { . . . };
BaseOpt<Alloc,Node*> head_;
public:
explicit list(Alloc const& a = Alloc())
: head_(a,0) { . . . }
. . .
};